/// <reference path="../_all.ts" />

module Application.Utility {
    export enum MessageType { info, warning, error, success }

    export class Message {
        public Title: string;
        public Type: number;
        public Text: string;
        public DateTime: Date;
        public ngClass: any;

        constructor(title: string, type: number, text: string) {
            this.Title = title;
            this.Type = type;
            this.Text = text;
            this.DateTime = new Date();
            switch(this.Type)
            {
                case MessageType.success:
                    this.ngClass = { 'text-success': true };
                    break;
                case MessageType.error:
                    this.ngClass = { 'text-danger': true };
                    break;
                case MessageType.warning:
                    this.ngClass = { 'text-warning': true };
                    break;
                default:
                    this.ngClass = { 'text-info': true };
            };
        }
    }

    export class Messages {
        public items: Message[];

        constructor() {
            this.items = [];
        }

        public AddAndGrowl(type: MessageType, title: string, text: string) {
            var classType = "info";
            switch (type) {
                case MessageType.success:
                    this.AddSuccess(title, text);
                    classType = 'success';
                    break;
                case MessageType.error:
                    this.AddError(title, text);
                    classType = 'error';
                    break;
                case MessageType.warning:
                    this.AddWarning(title, text);
                    classType = 'warning';
                    break;
                default:
                    this.AddInfo(title, text);
            };
            var jq = <any>$;
            if (jq['msgGrowl'] != null) {
                jq.msgGrowl({ title: title, text: text, type: classType});
            };
        }

        public AddMessage(title: string, type: number, text: string) {
            var msg = new Message(title, type, text);
            if (this.items == null) this.items = [];
            this.items.push(msg);
        }

        public AddInfo(title: string, text: string) {
            var msg = new Message(title, MessageType.info, text);
            if (this.items == null) this.items = [];
            this.items.push(msg);
        }

        public AddError(title: string, text: string) {
            var msg = new Message(title, MessageType.error, text);
            if (this.items == null) this.items = [];
            this.items.push(msg);
        }

        public AddSuccess(title: string, text: string) {
            var msg = new Message(title, MessageType.success, text);
            if (this.items == null) this.items = [];
            this.items.push(msg);
        }

        public AddWarning(title: string, text: string) {
            var msg = new Message(title, MessageType.warning, text);
            if (this.items == null) this.items = [];
            this.items.push(msg);
        }

        public Clear() {
            if (this.items != null && this.items.length > 0) {
                this.items.length = 0;
            };
        }
    }

}